package com.hdax.gulimall.ware.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyRabbitConfig {

    /**
     * 使用json序列化机制，进行消息转换
     *
     * @return
     */
    @Bean
    public Jackson2JsonMessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 创建交换机
     */
    @Bean
    public Exchange stockEventChange(){
            return new TopicExchange("stock-event-exchange",true,false);
    }


    /**
     * 创建队列
     */
    @Bean
    public Queue  stockReleaseStockQueue(){
        //队列名称，是否持久化，    是否自动删除
        return new Queue("stock.release.stock.queue",true,false,false);
    }


    @Bean
    public Queue stockDelayQueue(){
        /**
         * 死信路由：信死了交给那个交换机
         * 信死了，交换机要用的路由键
         * 消息的过期时间
         */
        Map<String,Object> map=new HashMap<>();
        map.put("x-dead-letter-exchange","stock-event-exchange");
        map.put("x-dead-letter-routing-key","stock.release");
        //订单过期时间两分钟
        map.put("x-message-ttl",12000);
        return new Queue("stock.delay.queue",true,false,false,map);
    }


    /**
     * 交换机和队列进行绑定
     * @return
     */
    @Bean
    public Binding stockReleaseBinding(){
        Binding binding = new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.release",null);
        return binding;
    }

    @Bean
    public Binding stockLockedBinding(){
        Binding binding=new Binding("stock.delay.queue", Binding.DestinationType.QUEUE,"stock-event-exchange","stock.locked",null);
        return binding;
    }
}
